/**
 * 文件名：Test.java
 *
 * 创建人：Elvin Zhao - 279152260@qq.com
 *
 * 创建时间：2019年9月27日 下午5:14:18
 *
 * 版权所有：FNII
 */
package com.z100h.supplychain.order.service;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;

import com.alibaba.druid.util.StringUtils;
import com.z100h.supplychain.order.entity.ErpSkuDetail;
import com.z100h.supplychain.order.entity.ErpSpecSku;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author Elvin Zhao - 279152260@qq.com
 * @version 1.0 Created on 2019年9月27日 下午5:14:18
 */
public class Test2 {

	public static void main(String[] args) {

		Map<String, Set<String>> aaa = new HashMap<String, Set<String>>();

		Set<String> bb = new HashSet<String>();
		bb.add("1");
		aaa.put("a", bb);
		Set<String> bbb = aaa.get("a");
		System.err.println(bbb);
		bb.add("2");
		System.err.println(bbb);
		bbb.add("3");
		System.err.println(aaa.get("a"));
	}

	/**
	 * 
	 */
	public Test2() {

		List<ErpSkuDetail> all = new ArrayList<ErpSkuDetail>();

		// productSpec-summaryId-orderAmount
		Map<String, Map<String, Map<String, Integer>>> stockroomIdProductSpecSummaryIdAmount = new HashMap<String, Map<String, Map<String, Integer>>>();

		// productSpec-summaryId-erpsku
		Map<String, Map.Entry<String, Set<ErpSpecSku>>> proSpecSummaryIdSkuMap = new HashMap<String, Map.Entry<String, Set<ErpSpecSku>>>();

		/**
		 * 以上为原始、中间数据结构
		 * 
		 * 以下为目标数据结构
		 */

		// productSpec集合
		Set<String> productSpecSet = new HashSet<>();

		// productSpec-productSpecName/picUrl
		Map<String, ErpSkuDetail> productSpecNameMap = new HashMap<String, ErpSkuDetail>();

		// stockroomId-productSpec-orderAmount
		Map<String, Map<String, Integer>> stockroomIdProductSpecAmountMap = new HashMap<String, Map<String, Integer>>();

		// productSpec-erpsku
		Map<String, Set<ErpSpecSku>> proSpecSkuMap = new HashMap<String, Set<ErpSpecSku>>();

		// stockroom集合
		Set<String> stockroomIdSet = new HashSet<>();
		Map<String, String> stockroomIdNameMap = new HashMap<String, String>();

		if (CollectionUtils.isNotEmpty(all)) {
			for (ErpSkuDetail erpSkuDetail : all) {

				addProductSpecSummaryIdAmount(stockroomIdProductSpecSummaryIdAmount, erpSkuDetail);
				addProductSpecSkuAmount(proSpecSummaryIdSkuMap, erpSkuDetail);
				productSpecSet.add(erpSkuDetail.getProductSpecGroupKey());
				stockroomIdSet.add(erpSkuDetail.getStockroomId());
				stockroomIdNameMap.putIfAbsent(erpSkuDetail.getStockroomId(), erpSkuDetail.getStockroomName());
				productSpecNameMap.putIfAbsent(erpSkuDetail.getProductSpecGroupKey(), erpSkuDetail);
			}
		}

		for (String productSpecKey : productSpecSet) {
			proSpecSkuMap.put(productSpecKey, proSpecSummaryIdSkuMap.get(productSpecKey).getValue());
		}

		for (String stockroomIdKey : stockroomIdSet) {

			Map<String, Map<String, Integer>> stockProSpecSummaryIdsAmount = stockroomIdProductSpecSummaryIdAmount
					.get(stockroomIdKey);

			Map<String, Integer> productSpecAmountMap = stockroomIdProductSpecAmountMap.get(stockroomIdKey);

			if (productSpecAmountMap == null) {
				productSpecAmountMap = new HashMap<String, Integer>();
				stockroomIdProductSpecAmountMap.put(stockroomIdKey, productSpecAmountMap);
			}

			for (String productSpeckey : stockProSpecSummaryIdsAmount.keySet()) {
				Integer total = 0;
				for (Integer summaryIdAmount : stockProSpecSummaryIdsAmount.get(productSpeckey).values()) {
					total += summaryIdAmount;
				}
				productSpecAmountMap.put(productSpeckey, total);
			}

		}

	}

	/**
	 * 
	 * productSpec下某个serverSummaryId的erpSpecSku
	 * 
	 * @param proSpecSkuMap
	 * @param erpSkuDetail
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年9月28日 下午3:49:49
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private static void addProductSpecSkuAmount(Map<String, Map.Entry<String, Set<ErpSpecSku>>> proSpecSummaryIdSkuMap,
			ErpSkuDetail erpSkuDetail) {
		String productSpecGroupKey = erpSkuDetail.getProductSpecGroupKey();
		Map.Entry<String, Set<ErpSpecSku>> summaryIdErpSpecSku = proSpecSummaryIdSkuMap.get(productSpecGroupKey);

		Set<ErpSpecSku> erpSpecSkuSet = null;

		if (summaryIdErpSpecSku == null) {

			erpSpecSkuSet = new HashSet<ErpSpecSku>();
			erpSpecSkuSet.add(erpSkuDetail.getErpSpecSku());
			summaryIdErpSpecSku = new AbstractMap.SimpleEntry(erpSkuDetail.getServiceTypeIdSummaryIdGroupKey(),
					erpSpecSkuSet);
			proSpecSummaryIdSkuMap.put(productSpecGroupKey, summaryIdErpSpecSku);

		} else {

			if (StringUtils.equals(summaryIdErpSpecSku.getKey(), erpSkuDetail.getServiceTypeIdSummaryIdGroupKey())) {
				erpSpecSkuSet = summaryIdErpSpecSku.getValue();
				erpSpecSkuSet.add(erpSkuDetail.getErpSpecSku());
			} else {
				return;
			}

		}

	}

	/**
	 * 累积数量用，去重serverSummaryId
	 * 
	 * @param productSpecSummaryIdAmount
	 * @param erpSkuDetail
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年9月28日 下午3:22:35
	 */
	private static void addProductSpecSummaryIdAmount(
			Map<String, Map<String, Map<String, Integer>>> allProductSpecSummaryIdAmount, ErpSkuDetail erpSkuDetail) {

		String stockroomId = erpSkuDetail.getStockroomId();
		String productSpecGroupKey = erpSkuDetail.getProductSpecGroupKey();

		Map<String, Map<String, Integer>> stockProSpecSummaryIdsAmount = allProductSpecSummaryIdAmount.get(stockroomId);
		Map<String, Integer> summaryIdsAmount = null;

		if (stockProSpecSummaryIdsAmount == null) {
			// summaryId层
			summaryIdsAmount = new HashMap<String, Integer>();
			summaryIdsAmount.put(erpSkuDetail.getServiceTypeIdSummaryIdGroupKey(), erpSkuDetail.getOrderAmount());

			// productSpec-summaryId层
			stockProSpecSummaryIdsAmount = new HashMap<String, Map<String, Integer>>();
			stockProSpecSummaryIdsAmount.put(productSpecGroupKey, summaryIdsAmount);

			// stockroomId-productSpec-summaryId层
			allProductSpecSummaryIdAmount.put(stockroomId, stockProSpecSummaryIdsAmount);

		} else {

			summaryIdsAmount = stockProSpecSummaryIdsAmount.get(productSpecGroupKey);
			if (summaryIdsAmount == null) {
				summaryIdsAmount = new HashMap<String, Integer>();
				stockProSpecSummaryIdsAmount.put(productSpecGroupKey, summaryIdsAmount);
			}
			summaryIdsAmount.putIfAbsent(erpSkuDetail.getServiceTypeIdSummaryIdGroupKey(),
					erpSkuDetail.getOrderAmount());

		}
	}

}
